/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.carlom.stickyheader.example.slice;

import it.carlom.stickyheader.ResourceTable;
import it.carlom.stickyheader.example.fragment.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;

/**
 * 主AbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {

    private MainFraction mMainFraction;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mMainFraction = new MainFraction();
        mMainFraction.setItemClickCall(position -> {
            switch (position) {
                case 0:
                    loadFragment(new SimpleStikkyFragment());
                    break;
                case 1:
                    loadFragment(new ParallaxStikkyFragment());
                    break;
                case 2:
                    loadFragment(new ActionBarImageFragment());
                    break;
                case 3:
                    loadFragment(new IO2014HeaderFragment());
                    break;
                case 4:
                    loadFragment(new RecyclerStikkyFragment());
                    break;
                case 5:
                    loadFragment(new SimpleScrollViewFragment());
                    break;
                case 6:
                    loadFragment(new TargetStikkyFragment());
                    break;
            }
        });
        loadFragment(mMainFraction);
    }

    boolean back = false;

    /**
     * 加载 Fraction
     * @param fragment Fraction
     */
    public void loadFragment(final Fraction fragment) {
        back = true;
        FractionManager fractionManager = ((FractionAbility) getAbility()).getFractionManager();
        fractionManager.startFractionScheduler().replace(ResourceTable.Id_layout_container, fragment)
                .submit();
    }

    @Override
    protected void onBackPressed() {
        if (back) {
            back = false;
            loadFragment(mMainFraction);
            return;
        }
        super.onBackPressed();
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
